<template>
  <div class="uploadImg">
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      :http-request="httpRequest"
    >
      <img
        v-if="imageUrl"
        v-imgError="defaultImg"
        :src="imageUrl"
        class="avatar"
        @click.stop="showBigImage"
      >
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
    <div v-if="imageUrl" class="close" @click="closeEvent">X</div>
    <!-- 预览图片 -->
    <el-dialog title="图片预览" :visible.sync="ishow">
      <div style="text-align: center">
        <img class="img" :src="imageUrl" alt="">
      </div>
    </el-dialog>
    <transition name="xxx">
      <el-progress v-if="showProgress" :percentage="percentage" />
    </transition>
  </div>
</template>
<script>
import Cos from 'cos-js-sdk-v5'
const cos = new Cos({
  SecretId: 'AKIDrXx72h6D5h25rNcZFfneEXZKnzvax3tS',
  SecretKey: 'JLCn69vQeaGgpgGZcOt62h5C4fy0mHVS'
})
export default {
  name: 'UpdateImg',
  model: {
    prop: 'imageUrl'
  },
  props: {
    imageUrl: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      defaultImg: require('@/assets/common/head.jpg'),
      ishow: false,
      showProgress: false, // 是否显示进度条
      percentage: 0 // 进度条的变量值
      // imageUrl: ''
    }
  },
  computed: {},
  created() {},
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isPng = file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isPng) {
        this.$message.error('上传头像图片只能是 png 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isPng && isLt2M
    },
    httpRequest(res) {
      // 上传时显示进度条
      this.showProgress = true
      // 且设置进度条的值为0
      this.percentage = 0
      cos.putObject(
        {
          Bucket: 'first-1308460148', // 存储桶名称
          Region: 'ap-chongqing', // 存储桶所在地域
          Key: res.file.uid + '' /* 必须是字符串 */,
          StorageClass: 'STANDARD', // 固定值
          Body: res.file, // 上传文件对象
          onProgress: (progressData) => {
            // 上传进度
            this.percentage = progressData.percent * 100
            // progressData.percent === 1时 表示上传完成
            if (progressData.percent === 1) {
              setInterval(() => {
                this.showProgress = false
              }, 1000)
            }
            console.log(JSON.stringify(progressData))
          }
        },
        (err, data) => {
          console.log(err || data)
          console.log('this', this)
          // this.imageUrl = 'http://' + data.Location
          this.$emit('input', 'http://' + data.Location)
        }
      )
    },
    // 删除图片功能
    closeEvent() {
      this.$emit('input', '')
    },
    // 点击图片显示图片预览
    showBigImage() {
      this.ishow = true
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
::v-deep .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.uploadImg {
  position: relative;
  width: 178px;
  &:hover {
    .close {
      display: block;
    }
  }
  .close {
    display: none;
    position: absolute;
    top: 5px;
    right: 5px;
    cursor: pointer;
  }
  .img {
    width: 100%;
  }
}
.xxx-enter-active,
.xxx-leave-active {
  transition: all 0.5s;
}
.xxx-enter,
.xxx-leave-to {
  opacity: 0;
}
</style>
